package com.ssl.rms.controller.system;

import com.ssl.rms.controller.BaseController;
import com.ssl.rms.entity.Menu;
import com.ssl.rms.entity.MenuExample;
import com.ssl.rms.entity.TreeNode;
import com.ssl.rms.service.MenuService;
import com.ssl.rms.util.TreeNodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 描述：资源管理，@CrossOrigin解决跨域访问
 *
 * @author ssl
 * @create 2018/04/02 15:42
 */
@Controller
@RequestMapping("sys/menu")
@CrossOrigin(origins = "*", maxAge = 3600)
public class MenuController extends BaseController {
    @Autowired
    private MenuService menuService;

    /**
     * 首页
     *
     * @param model
     * @return
     */
    @RequestMapping(value = {"", "/", "index"})
    public String index(Model model) {
        return "system/menu/index";
    }

    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "name", required =
            false) String name, Model model) {
        model.addAttribute("id", id);
        model.addAttribute("name", name);
        return "system/menu/create";
    }

    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public String detail(@PathVariable(value = "id") Long id, Model model) {
        model.addAttribute("menu", menuService.selectByPrimaryKey(id));
        return "system/menu/detail";
    }

    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable(value = "id") Long id, Model model) {
        model.addAttribute("menu", menuService.selectByPrimaryKey(id));
        return "system/menu/update";
    }

    /**
     * 获取当前用户拥有的资源
     *
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String, Object> findAll() {
        List<Menu> menus = menuService.selectAll();
        List<TreeNode> data;
        try {
            /** 如果此处data为string类型的json字符串，则页面上接收会报错，用list则不会 */
            data = TreeNodeUtils.getTreeNode(menus);
        } catch (Exception e) {
            return FAIL(e);
        }
        return SUCCESS(data);
    }

    /**
     * 查询菜单资源信息
     *
     * @param menuId
     * @return
     */
    @RequestMapping(value = "get/{id}")
    @ResponseBody
    public Map<String, Object> getMenu(@PathVariable(value = "id") Long menuId) {
        Menu menu = menuService.selectByPrimaryKey(menuId);
        if (null == menu) {
            return FAIL("500", "没有找到对应的资源");
        }
        return SUCCESS(menu);
    }

    /**
     * 更新菜单资源
     *
     * @param menu
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> update(Menu menu) {
        Menu oldMenu = menuService.selectByPrimaryKey(menu.getId());
        if (null == oldMenu) {
            return FAIL("500", "没有找到对应的资源");
        }
        menuService.updateByPrimaryKeySelective(menu);
        return SUCCESS(menu);
    }

    /**
     * 创建菜单资源
     *
     * @param menu
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(Menu menu) {
        menuService.insertSelective(menu);
        return SUCCESS(menu);
    }

    /**
     * 删除菜单资源
     *
     * @param menuId
     * @return
     */
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(@PathVariable(value = "id") Long menuId) {
        Menu menu = menuService.selectByPrimaryKey(menuId);
        if (null == menu) {
            return FAIL("500", "没有找到对应的资源");
        }
        menuService.deleteByPrimaryKey(menuId);
        return SUCCESS();
    }
}
